package com.csxy.lsh.controller;

import com.csxy.lsh.JwtUtils;
import com.csxy.lsh.R;
import com.csxy.lsh.UcenterMember;
import com.csxy.lsh.entity.vo.LoginVo;
import com.csxy.lsh.entity.vo.RegisterVo;
import com.csxy.lsh.entity.vo.UcenterMemberVo;
import com.csxy.lsh.exception.MyException;
import com.csxy.lsh.MD5;
import com.csxy.lsh.service.UcenterService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/serviceUcenter/ucenter")
public class UcenterController {
    @Autowired
    private UcenterService ucenterService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {
        System.out.println(loginVo.getPassword() + loginVo.getPhone());
        String token = ucenterService.login(loginVo);
        return R.ok().data("token", token);
    }


        @PostMapping("/register")
        public R register(@RequestBody RegisterVo registerVo ){
            ucenterService.register(registerVo);
            return R.ok();
        }

        //获取用户信息
    @GetMapping({"/getUserInfoForJwt", "/getUserInfoForJwt/"})
    public R getUserInfoForJwt(HttpServletRequest  request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterService.getById(memberId);
        UcenterMemberVo memberVo = new UcenterMemberVo();
        org.springframework.beans.BeanUtils.copyProperties(member, memberVo);
        return R.ok().data("userInfo", memberVo);
    }

    @PostMapping("/getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoById(@PathVariable String memberId){
        UcenterMember member =ucenterService.getById(memberId);
        if (member == null){
            throw  new MyException(20001,"用户不存在");



        }
        UcenterMemberVo memberVo =new UcenterMemberVo();
        BeanUtils.copyProperties(member,memberVo);

        return memberVo;
    }

    //根据日期获取那天注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = ucenterService.countRegister(day);
        return R.ok().data("countRegister", count);
    }

    // 新增：更新個人信息（昵稱、頭像）
    @PostMapping("/updateProfile")
    public R updateProfile(@RequestBody UcenterMemberVo memberVo, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (memberId == null) {
            return R.error().message("未登錄");
        }
        UcenterMember member = ucenterService.getById(memberId);
        if (member == null) {
            return R.error().message("用戶不存在");
        }
        if (memberVo.getNickname() != null) member.setNickname(memberVo.getNickname());
        if (memberVo.getAvatar() != null) member.setAvatar(memberVo.getAvatar());
        ucenterService.updateById(member);
        return R.ok().message("更新成功");
    }

    // 新增：修改密碼
    @PostMapping("/changePassword")
    public R changePassword(@RequestBody Map<String, String> body, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (memberId == null) {
            return R.error().message("未登錄");
        }
        UcenterMember member = ucenterService.getById(memberId);
        if (member == null) {
            return R.error().message("用戶不存在");
        }
        String oldPassword = body.get("oldPassword");
        String newPassword = body.get("newPassword");
        if (!MD5.encrypt(oldPassword).equals(member.getPassword())) {
            return R.error().message("舊密碼錯誤");
        }
        member.setPassword(MD5.encrypt(newPassword));
        ucenterService.updateById(member);
        return R.ok().message("密碼修改成功");
    }
}
